package towerz.component;

import java.io.IOException;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import towerz.system.BitmapFont;
import towerz.system.StateManager;
import towerz.system.Utilities;

public class Button {
	public static final int STATE_NONE 	= 0;
	public static final int STATE_SELECTED 	= 1;
	public static final int STATE_PRESSED 	= 2;

	public Button(){ //Default ctor
	}

	public Button(int id, String image, int x, int y, int width, int height) {
		//Super
		this(id, image, x, y, width, height, "");
	}

	public Button(int id, String image, int x, int y, int row) {
		//Super
		this(id, image, x, y, row, "");
	}
	
	public Button(int id, String image, int x, int y, int row, String text) {
		//Set data
		m_X 		= x;
		m_Y 		= y;
		m_ID		= id;
		m_Text		= text;
		m_Width		= 0;
		m_Height 	= 0;
		m_State		= STATE_NONE;
		m_Images	= null;
		m_TextImg	= BitmapFont.getFont(BitmapFont.TITLE).renderTransparentText(text, 0x00000000);

		//Set default colors
		m_Colors = new int[] { 0xBDBDBD, 0xFFFFFF, 0xBDBDBD };
		
		//Create image
		try {
			//Set variable
			m_Images		= new Image[row];
			Image Source 	= Image.createImage(image);
			
			//Calculate size
			m_Width 	= Source.getWidth();
			m_Height	= Source.getHeight() / row;
			
            //Recalculate X to center it
            m_X	= (StateManager.instance().getWidth() - m_Width) / 2;
            
			//Create 3 frames
			for (int i = 0; i < m_Images.length; i++)
				m_Images[i] = Image.createImage(Source, 0, i * m_Height, m_Width, m_Height, 0);			
		} catch (IOException e) {}
	}
	
	public Button(int id, String image, int x, int y, int width, int height, String text) {
		//Set data
		m_X 		= x;
		m_Y 		= y;
		m_ID		= id;
		m_Text		= text;
		m_Images	= null;
		m_State		= STATE_NONE;
		m_Width		= width;
		m_Height	= height;
		m_TextImg	= BitmapFont.getFont(BitmapFont.TITLE).renderTransparentText(text, 0x00000000);

		//Set default colors
		m_Colors = new int[] { 0xBDBDBD, 0xFFFFFF, 0xBDBDBD };
		
		//Create image
		try {
			//Set variable
			m_Images		= new Image[3];
			Image Source 	= Image.createImage(image);
			
			//Create 3 frames
			for (int i = 0; i < m_Images.length; i++)
				m_Images[i] = Image.createImage(Source, 0, i * m_Height, m_Width, height, 0);			
		} catch (IOException e) {}
	}

	public int getX() {
		return m_X;
	}

	public void setX(int X) {
		m_X = X;
	}

	public int getY() {
		return m_Y;
	}

	public void setY(int Y) {
		m_Y = Y;
	}

	public int getID() {
		return m_ID;
	}
	
	public int getWidth() {
		return m_Width;
	}
	
	public int getHeight() {
		return m_Height;
	}
	
	public void setText(String text) {
		m_Text = text;
		m_TextImg = BitmapFont.getFont(BitmapFont.TITLE).renderTransparentText(text, 0x00000000);
	}
	
	public void setState(int state) {
		//set state
		m_State = state;

		if(m_State == Button.STATE_PRESSED){
			//Pressed sound effect
			StateManager.instance().playSFX("/select.wav");
		}
	}

	public void setImage(String imgPath){ //DUMMY METHOD
		//Create image
		try {
			//Set variable
			m_Images		= new Image[3];
			Image Source 	= Image.createImage(imgPath);

			//Calculate size
			m_Width 	= Source.getWidth();
			m_Height	= Source.getHeight() / 3;

			//Create 3 frames
			for (int i = 0; i < m_Images.length; i++)
				m_Images[i] = Image.createImage(Source, 0, i * m_Height, m_Width, m_Height, 0);
		} catch (IOException e) {}
	}
	
	public void setColors(int[] colors) {
		if (colors.length == 3) m_Colors = colors; 
	}
	
	public void draw(Graphics g) {
		//Draw based on the state
		if (m_Images != null && m_Images[m_State] != null)
			g.drawImage(m_Images[m_State], m_X, m_Y, Graphics.LEFT | Graphics.TOP);
		
		//Set text color based on state
		g.setColor(m_Colors[m_State]);

		g.drawImage(m_TextImg, m_X + (m_Width / 2), m_Y + (m_Height / 2) + 2, Graphics.HCENTER | Graphics.VCENTER);
	}
	
	//Data
	protected int	m_X;
	protected int 	m_Y;
	protected int	m_ID;
	protected int 	m_State;
	protected int 	m_Width;
	protected int 	m_Height;
	protected int[] m_Colors;
	
	//Display
	protected String	m_Text;
	protected Image[]	m_Images;
	protected Image		m_TextImg;
}
